<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        html {
            height: 100%;
        }
        body {
            height: 100%;
        }
        .container {
            height: 100%;
            background-image: linear-gradient(to right, #999999, #330867);
        }
        .login-wrapper {
            background-color: bisque;
            width: 358px;
            height: 588px;
            border-radius: 15px;
            padding: 0 50px;
            position: relative;
            left: 50%;
            top: 50%;
            transform: translate(-50%,-50%);
        }
        .header {
            font-size: 38px;
            font-weight: bold;
            text-align: center;
            line-height: 200px;
        }
        .input-item {
            display: block;
            width: 100%;
            margin-bottom: 20px;
            border: 0;
            padding: 10px;
            border-bottom: 1px solid rgb(128,125,125);
            font-size: 15px;
            outline: none;
        }
        .input-item::placeholder {
            text-transform: uppercase;
        }
        .btn {
            text-align: center;
            padding: 10px;
            width: 100%;
            margin-top: 40px;
            background-image: linear-gradient(to right,#a6c1ee, #fbc2eb);
            color: #fff;
        }
        .msg {
            text-align: center;
            line-height: 88px;
        }
        a {
            text-decoration-line: none;
            color: #abc1ee;
        }
    </style>
    <script type="text/javascript" src="/js/jquery.1.11.3.min.js"></script>
    <script>
     $(function () {
         $(".btn").on('click', function () {
             var token = $("input[name='_csrf']").val();  //获取CSRF令牌, 否则请求报403
             var uname = $("input[name*='username']").val(); //获取用户名
             var pwd = $("input[name*='password']").val();  //获取密码
              $.ajax({
                 url: "/login",
                 contentType: "application/json",
                 type: "post",
                 headers: {"X-CSRF-TOKEN":token},
                 data: JSON.stringify({
                     userName:uname,
                     password:pwd
                 }),
                 success: function (data) {
                    if(data.access_token != "" && data.access_token != null){
                       window.location.href = "/order/index?access_token=" + data.access_token;
                       console.log("*********************************")
                    }else if(data.error == "unauthorized"){
                       alert("没有授权, 不能访问")
                    }else if(data.error == "invalid_grant"){
                       alert("错误密码, 不能访问")
                    }else if(data.error == "invalid_client"){
                       alert("无效的客户端")
                    }
                 }
              })

         })
     })

    </script>

</head>
<body>
<div class="container">
    <div class="login-wrapper">
        <div class="header">Login</div>
        <form th:action="@{/login}" method="post">
            <div class="form-wrapper">
                <input type="text" name="username" placeholder="username" class="input-item">
                <input type="password" name="password" placeholder="password" class="input-item">
                <div class="btn">Login</div>
            </div>
        </form>
        <div class="msg">
            Don't have account?
            <a href="#">Sign up</a>
        </div>
    </div>
</div>
</body>

</html>